using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;

namespace demo
{
    public class QrCodeHelper
    {

        /// <summary>
        /// 生成二维码图片
        /// 256 * 256
        /// </summary>
        /// <param name="content">输入：code_url</param>
        /// <returns>输出：二维码图片</returns>
        public static Bitmap CreateQRCode(string content)
        {

            return CreateQRCode(content, 256, 256);
        }


        /// <summary>
        /// 生成二维码图片
        /// </summary>
        /// <param name="content">输入：code_url</param>
        /// <returns>输出：二维码图片</returns>
        public static Bitmap CreateQRCode(string content, int width, int height)
        {
            CodeHelper helper = new CodeHelper();

            BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
            Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
            hints.Add(EncodeHintType.MARGIN, 0);//边距
            hints.Add(EncodeHintType.CHARACTER_SET, helper.CHARSET);
            hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);//设置容错等级，不设置这个，可以添加logo图片
            Bitmap bitmap = helper.encode(content, width, height, barcodeFormat, hints);
            return bitmap;
        }
    }
}
